package com.hzdl.flower.controller;

import com.github.pagehelper.PageInfo;
import com.hzdl.flower.bean.Admin;
import com.hzdl.flower.service.AdminService;
import com.hzdl.flower.util.HZDLJsonResult;
import com.hzdl.flower.util.HzdlJsonAjax;
import com.hzdl.flower.vo.AdminVo;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("admin")
public class AdminController {
    @Autowired
    private AdminService as;


    @RequestMapping("adminInit")
    private String adminInit(){
        return "admin/admin" ;
    }

    @ResponseBody
    @RequestMapping("findAdmin")
    public HZDLJsonResult findAdmin(AdminVo adminVo){
        PageInfo p = as.findAdmin(adminVo);
        return new HZDLJsonResult(0,"",p.getTotal(),p.getList());
    }

    @RequestMapping("delete")
    @ResponseBody
    public HZDLJsonResult delete(String id1){
        int row = as.deleteById(id1);
        if (row == 0){
            return HZDLJsonResult.DELETE_ERROR;
        }else {
            return HZDLJsonResult.DELETE_SUCCESS;
        }
    }

    @ResponseBody
    @RequestMapping("delAll")
    public HZDLJsonResult deAll(String ids){
        String[] arr = ids.split(",");
        Integer result=0;
        for (int a=0;a<arr.length;a++){
            result += as.deleteById((arr[a]));
        }
        if (result==0){
            return HZDLJsonResult.DELETE_ERROR;
        }else {
            return  HZDLJsonResult.DELETE_SUCCESS;
        }
    }

    @RequestMapping("checkExitChildren")
    @ResponseBody
    public HZDLJsonResult checkExitChildren(String id){
        Admin id1 = as.findById(id);
        return new HZDLJsonResult(0,"",id1);
    }



    @RequestMapping("add")
    @ResponseBody
    public HZDLJsonResult add(AdminVo adminVo ){

        //区别修改还是添加，使用id1来判断
        if (adminVo.getId1()== null || adminVo.getId1().isEmpty()){
            int i = as.addAdmin(adminVo);
            if (i == 0){
                return HZDLJsonResult.ADD_ERROR;
            }else {
                return HZDLJsonResult.ADD_SUCCESS;
            }

        }else {
            int i = as.updateAdmin(adminVo);
            if (i == 0) {
                return HZDLJsonResult.UPDATE_ERROR;
            }else {
                return HZDLJsonResult.UPDATE_SUCCESS;
            }
        }

    }


}
